from rest_framework.decorators import action
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from apps.goods.models import SPUSpecification, SPU
from apps.meiduo_admin.utils import NumPage
from apps.meiduo_admin.serialziers.goods import GoodsSpecSerializer


class GoodsSpecViews(ModelViewSet):
    # 指定查询集
    queryset = SPUSpecification.objects.all()
    # 指定序列化器
    serializer_class = GoodsSpecSerializer
    # 指定权限
    permission_classes = [IsAdminUser]
    # 指定分页
    pagination_class = NumPage

    # 自定义方法获取简单spu数据
    def simple(self, request):
        # 查询spu表
        spus = SPU.objects.all()
        # 返回spu的id和名称
        spu_list = []
        for spu in spus:
            spu_list.append({
                'id': spu.id,
                'name': spu.name
            })
        return Response(spu_list)
